<%@ page import="entity.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %><%--
  Created by IntelliJ IDEA.
  User: User
  Date: 2024/5/15
  Time: 15:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>EL表达式</title>
    </head>
    <body>
        <%
            //模拟数据
            pageContext.setAttribute("msg","Hello pageContext");
            request.setAttribute("msg","Hello request");
            session.setAttribute("msg","Hello session");
            application.setAttribute("msg","Hello application");
            User user=new User(1,"admin","123");
            User user2=new User(2,"alice","123");
            User user3=new User(3,"jack","123");
            request.setAttribute("user",user);
            List<User> users=new ArrayList<User>();
            users.add(user);
            users.add(user2);
            users.add(user3);
            request.setAttribute("users",users);

            Map<String,User> map=new HashMap<String,User>();
            map.put("u1",user);
            map.put("u2",user2);
            map.put("u3",user3);
            request.setAttribute("map",map);
        %>
        <h2>访问作用域</h2>
        <!--
            使用EL表达式隐式对象中的作用域对象，可以操作对应作用域的数据
            表表达式的内容:${对象.key}
            ${key}根据作用从小到大进行查找，一旦查找到直接返回数据
            如果四大作用域均没有对应key的数据，页面则什么都不显示，相当于是空字符串
            如果数据是一个对象，可以直接使用对象名.属性名即可
        -->
        pageContext:${pageScope.msg}<br>
        request:${requestScope.msg}<br>
        session:${sessionScope.msg}<br>
        application:${applicationScope.msg}<br>
        msg:${msg}
        <br>
        aaa:${aaa}
        <h2>访问对象</h2>
        <!--在EL表达式中类似于java中的get/set方法,此时可以直接省略get关键字，方法首字母小写-->
        ${requestScope.user.id}--${requestScope.user.username}--${requestScope.user.password}<br>
        ${user.id}--${user.username}--${user.password}
        <br>
        应用名:
        <%
            HttpServletRequest req= (HttpServletRequest) pageContext.getRequest();
            String path=req.getContextPath();
            out.print(path);
        %>
        <br>
        使用EL表达式表示应用名：${pageContext.request.contextPath}
        <br>
        <%
            String id=request.getParameter("id");
            out.print(id);
            String username=request.getParameter("username");
            out.print(username);
        %>
        <br>
        ${param.id}--${param.username}
        <!--
            获取List集合的数据，本质上就是get方法
        -->
        ${users[1].id}--${users[1].username}--${users[2].password}
        <br>
        <!--
            Map集合的数据
            map[key]
            map.key:该方式的key不能以数字开头，即必须符合编码规范
        -->
        ${map["u1"].id}-- ${map["u1"].username}-- ${map["u1"].password}
        <br>
        ${map.u2.id}-- ${map.u2.username}-- ${map.u2.password}
        <br>
       <!-- EL表示的运算类似于js-->

       <!-- + -  *  /  % -->
        3+2=${3+2}
        <br>
        3-2=${3-2}
        <br>
        3/2=${3/2}
        <br>
        ${1=="1"}
        <br>
        ${"2" eq "1"}
        <br>
        ${"2" eq 2}
        <br>
        user是否为空:${empty user}
        <br>
        emp是否为空:${empty emp}
        <br>
        三目:${2==1?"checked":"no-checked"}
        <br>
        且:${true and false}
        <br>
        或:${true or false}


    </body>
</html>
